package cn.fulong.smebj.admin.controller;

import cn.fulong.frame.config.Result;
import cn.fulong.smebj.entity.Information;
import cn.fulong.smebj.entity.Maths;
import cn.fulong.smebj.mapper.InformationMapper;
import cn.fulong.smebj.mapper.MathsMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Api("竞赛资讯")
@RestController
@RequestMapping("/info")
public class InfoController {
    @Autowired
    private InformationMapper informationMapper;


    @ApiOperation("管理列表")
    @RequestMapping(value = "/list")
    public Result<Object> list(Integer pageNo, Integer pageSize) {
        IPage<Map<String,Object>> iPage = new Page<>(Optional.ofNullable(pageNo).orElse(0), pageSize);
        IPage result = informationMapper.queryList(iPage);
        return Result.success(result);
    }


    @ApiOperation("保存资讯")
    @PostMapping(value = "/save")
    public Result<Object> save(String title,String content) {
        Information info = new Information();
        info.setContent(content);
        info.setTitle(title);
        info.setCreateTime(new Date(System.currentTimeMillis()));
        informationMapper.insert(info);
        return Result.success();
    }

    @ApiOperation("更新资讯")
    @PostMapping(value = "/update")
    public Result<Object> update(Long id,String title,String content) {
        Information info = informationMapper.selectById(id);
        info.setContent(content);
        info.setTitle(title);
        info.setCreateTime(new Date(System.currentTimeMillis()));
        informationMapper.updateById(info);
        return Result.success();
    }

    @ApiOperation("删除资讯")
    @PostMapping(value = "/delete")
    public Result<Object> delete(Long id) {
        informationMapper.deleteById(id);
        return Result.success();
    }

}
